﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;

using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Library;
using VIT.Library.Web;
using VIT.DataHelper.DataAccess;
using VIT.Wedding;

namespace VIT.Web.Admin
{
    public partial class ProductList : VITComponent
    {
        public string genOption = string.Empty;
        Provider.Product obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new Provider.Product();
            loadCategory();
            if (!Page.IsPostBack)
            {
                Session["order"] = "Orders";
                loadLang();

                loadManufacturers();
                loadSuppliers();

                //LoadData(Session["order"].ToString());
            }
        }
        protected void Pager_PreRender(object sender, EventArgs e)
        {
            LoadData(Session["order"].ToString(), Request["CategoryId"], ddlNSX.SelectedValue, ddlNCC.SelectedValue, Convert.ToBoolean(Session["desc"]));
            ListView.DataBind();
        }

        #region Methods
        public void LoadData(string SortColumn = "", string CategoryId = "0", string ManufacturerId = "0", string SuppierId = "0", bool desc = true)
        {
            Provider.Product obj = new Provider.Product();
            obj.LanguageId = Session["language"].ToString();
            if (CategoryId == null) CategoryId = "0";
            if (ManufacturerId == null) ManufacturerId = "0";
            if (SuppierId == null) SuppierId = "0";

            DataTable table;
            if (CategoryId != "0")
            {
                Provider.Category c = new Provider.Category();
                c.LanguageId = obj.LanguageId;
                DataTable dt = c.GetAll();
                string categoryList = dt.GetChildId(CategoryId);
                table = obj.Filter(SortColumn, ManufacturerId, SuppierId, categoryList: categoryList, desc: desc);
            }
            else
                table = obj.Filter(SortColumn, ManufacturerId, SuppierId, desc: desc);

            ListView.DataSource = table;
        }

        private void loadLang()
        {
            ddlLang.DataSource = new Provider.Language().getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        private void loadCategory()
        {
            Provider.Category c = new Provider.Category();
            c.LanguageId = Session["language"].ToString();
            string selectedId = Request["CategoryId"];
            this.genOption = c.GetAll().GenTagOption("Id", "Title", "2", selected: selectedId);
        }

        private void loadManufacturers(string selected = "0")
        {
            Provider.ProductManufacturers m = new Provider.ProductManufacturers();
            var lstItem = m.GetAll();

            ddlNSX.Items.Clear();
            ddlNSX.Items.Add(new ListItem("Tất cả", "0"));
            foreach (DataRow row in lstItem.Rows)
            {
                ddlNSX.Items.Add(new ListItem(row["Name"].ToString(), row["Id"].ToString()));
            }

            ddlNSX.SelectedValue = selected;
        }

        private void loadSuppliers(string selected = "0")
        {
            Provider.ProductSupplier p = new Provider.ProductSupplier();
            var lstItem = p.GetAll();

            ddlNCC.Items.Clear();
            ddlNCC.Items.Add(new ListItem("Tất cả", "0"));
            foreach (DataRow row in lstItem.Rows)
            {
                ddlNCC.Items.Add(new ListItem(row["Name"].ToString(), row["Id"].ToString()));
            }

            ddlNCC.SelectedValue = selected;
        }

        //private void LoadColors()
        //{
        //    ddlColor.DataValueField = "ColorWedID";
        //    ddlColor.DataTextField = "Name";
        //    var dtColor = VITData.GetQuery<TColorWedding>().ToArray();
        //    ddlColor.DataSource = dtColor;
        //    ddlColor.DataBind();
        //    ddlColor.Items.Insert(0, new ListItem("---Chọn màu---", "-1"));
        //}

        //private void LoadTypes()
        //{
        //    ddlType.DataValueField = "TypeWedID";
        //    ddlType.DataTextField = "Name";
        //    var dtType = VITData.GetQuery<TTypeWedding>().ToArray();
        //    ddlType.DataSource = dtType;
        //    ddlType.DataBind();
        //    ddlType.Items.Insert(0, new ListItem("---Chọn kiểu---", "-1"));
        //}

        public void deleteList(string[] listId)
        {
            foreach (string id in listId)
            {
                obj.Delete(1, int.Parse(id));
            }
        }

        public void PublishList(int pub, string[] listId)
        {
            foreach (string id in listId)
                obj.Publish(pub, int.Parse(id));
        }
        #endregion

        #region Events
        protected void Delete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            deleteList(check);
        }

        protected void Insert_Click(object sender, EventArgs e)
        {
            RedirectComponent("Product", "ProductEdit");
        }

        protected void Published_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(1, check);
            //LoadData(Session["order"].ToString(), Request["CategoryId"], ddlNSX.SelectedValue, ddlNCC.SelectedValue);
        }

        protected void UnPublished_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(0, check);
            //LoadData(Session["order"].ToString(), Request["CategoryId"], ddlNSX.SelectedValue, ddlNCC.SelectedValue);
        }

        protected void changePublish_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            int pub = (id[1] == "True") ? 0 : 1;
            obj.Publish(pub, int.Parse(id[0]));
            //LoadData(Session["order"].ToString(), Request["CategoryId"], ddlNSX.SelectedValue, ddlNCC.SelectedValue);
        }

        protected void updateOrder_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            obj.Order(id[1], int.Parse(id[0]));
            //LoadData(Session["order"].ToString(), Request["CategoryId"], ddlNSX.SelectedValue, ddlNCC.SelectedValue);
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = ddlLang.SelectedValue;
            //LoadData(Session["order"].ToString(), Request["CategoryId"], ddlNSX.SelectedValue, ddlNCC.SelectedValue);
        }

        protected void Filter_Click(object sender, EventArgs e)
        {
            //LoadData(Session["order"].ToString(), Request["CategoryId"], ddlNSX.SelectedValue, ddlNCC.SelectedValue);
        }

        protected void sortedChange_OnClick(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            Session["desc"] = !Convert.ToBoolean(Session["desc"]);
            Session["order"] = btn.Param.ToString();
        }
        #endregion
    }
}
